package com.itic.mall.mobile.cert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.cert.persistence.model.Cert;
import com.itic.mall.system.cert.persistence.model.CertExample;
import com.itic.mall.system.cert.service.CertService;
import com.itic.mall.system.certproduct.persistence.model.CertProduct;
import com.itic.mall.system.certproduct.persistence.model.CertProductExample;
import com.itic.mall.system.certproduct.persistence.model.CertProductExample.Criteria;
import com.itic.mall.system.certproduct.service.CertProductService;
import com.itic.mall.system.product.persistence.model.Product;
import com.itic.mall.system.product.persistence.model.ProductOrderVo;
import com.itic.mall.system.product.service.ProductService;
import com.itic.mall.system.productsku.persistence.model.ProductSku;
import com.itic.mall.system.productsku.service.ProductSkuService;
import com.itic.mall.system.user.persistence.model.MallUser;
import com.itic.mall.system.useraddress.persistence.model.UserAddress;
import com.itic.mall.system.useraddress.persistence.model.UserAddressExample;
import com.itic.mall.system.useraddress.service.UserAddressService;


/**
 * 友惠家商城中心接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/cert")
public class MallCertInterface
     extends BaseMobileInterface {
   @Autowired
   private ProductService productService;
   @Autowired
   private ProductSkuService productSkuService;
   @Autowired
   private CertService certService;
   @Autowired
   private CertProductService certProductService;
   @Autowired
   private UserAddressService userAddressService;
   /**
    * 购物车进入
    * @return
    */
   @RequestMapping(value="/index")
   @ResponseBody
   public Map<String,Object> index(HttpServletRequest request,HttpServletResponse response,String token){
   	Map<String, Object> resultMap = new HashMap<String,Object>();
   	String result = FAIL;
   	String errorCode = PARAM_ERROR_CODE;
   	String errorMsg = PARAM_ERROR_MSG;
   	try{
   		if( StringHelper.isNotBlank(token) ){
   			MallUser user = getMallUserByToken(token);
   			if( null != user ){
   				Map<String, Object> dataMap = new HashMap<String,Object>();
   			    Cert cert = getUserCert(user.getId());
   				List<CertProduct> certProducts = getUserCertProduct(cert);
   	   			dataMap.put("totalCount", cert.getTotalCount());
   	   			dataMap.put("totalMoney", cert.getTotalMoney());
   	   			dataMap.put("products", certProducts);
   	   		    resultMap.put(DATA, dataMap);
	   	   		result = SUCCESS;
	   			errorCode = SUCCESS_ERROR_CODE;
	   	       	errorMsg = SUCCESS_ERROR_MSG; 
   			}else{
   				result = LOGIN;
   				errorCode = LOGIN_ERROR_CODE;
   		       	errorMsg = LOGIN_ERROR_MSG;    
   			}
   		}
       	
   	}catch(Exception e){
   		errorCode = SERVER_ERROR_CODE;
       	errorMsg = SERVER_ERROR_MSG;
       	logException("MallCompanyInterface", "phone", e.toString(),"获取客服电话",e);
   	}
   	resultMap.put(RESULT, result);
   	resultMap.put(ERROR_CODE, errorCode);
   	resultMap.put(ERROR_MSG, errorMsg);
   	isCrrossDomain(response);
   	return resultMap;
   } 
   
   
   
   /**
    * 添加商品款式到购物车
    * @return
    */
   @RequestMapping(value="/preJiesuan")
   @ResponseBody
   public Map<String,Object> preJiesuan( HttpServletRequest request,HttpServletResponse response,String token,String products,String marketUserProductId ){
	   Map<String, Object> resultMap = new HashMap<String,Object>();
	   	String result = FAIL;
	   	String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	   	try{
	   		//判断必要的参数
	   		if( StringHelper.isNotBlank(token) ){
	   			MallUser user = getMallUserByToken(token);
	   			if( null != user ){
	   				Cert cert = getUserCert(user.getId());
	   				List<CertProduct> certProducts = getUserCertProduct(cert);
	   			    //String products = "";
	   			    if( certProducts.size() > 0 ){
	   			    	/*for( CertProduct tmp: certProducts ){
	   			    		products += tmp.getProductId()+":"+tmp.getProductSkuId()+":"+tmp.getProductCount()+",";
	   			    	}*/
	   			    	String isHaveAddress = "0"; 
		   			    UserAddress mainAddress = null;
		   			    List<ProductOrderVo> productsDetail = new ArrayList<ProductOrderVo>();
		   			    String totalHongbao="0";//总优惠红包金额
		   			    String totalOriginPrice = "0";//商品总价格
		   			    String totalPostFee = "0";//商品总邮费
		   			    String totalPrice = "0";//商品总价
		   			    
		   				//查找用户有没有收货地址
		   				UserAddressExample example = new UserAddressExample();
		   				com.itic.mall.system.useraddress.persistence.model.UserAddressExample.Criteria criteria = example.createCriteria();
		   				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
		   				criteria.andUserIdEqualTo(user.getId());
		   				criteria.andIsMainEqualTo(DBConst.TRUE);
		   				List<UserAddress> list = userAddressService.list(example);
		   				if( list.size() > 0 ){
		   					isHaveAddress = "1";
		   					mainAddress = list.get(0);
		   				}
		   				
		   				String[] productsStr = products.split(",");
		   				
		   				if( productsStr.length > 0 ){
		   					for( int i=0; i<productsStr.length; i++ ){
		   						String[] temp = productsStr[i].split(":");
		   						String productId = "";
		   						String productSkuId = "";
		   						if( null != temp ){
		   							productId = temp[0];//商品ID
		   							productSkuId = temp[1];
		   						}
	   						
	   						Product product = productService.get(productId);
	   						product = filterSeckillPrice(product);
	   						ProductSku productSku = productSkuService.get(productSkuId);
	   						productSku=filterSeckillPrice(productSku);
	   						if( null != product ){
	   							ProductOrderVo vo = new ProductOrderVo();
	   							vo.setTotalCount(temp[2]);
	   							vo.setName(product.getName());
	   							vo.setMainImageUrl(product.getMainImageUrl());
	   							vo.setTotalFeedback(product.getTotalFeedback());
	   							vo.setPercentFeedback(product.getPercentFeedback()==""?"100%":product.getPercentFeedback());
	   							String productPrice = getProductPrice(vo.getProductOrginPrice(),user.getId(),product);
	   							vo.setProductPrice(productPrice);
	   							vo.setProductOrginPrice(productSku.getProductOrginPrice());
	   							vo.setProductHongbao(getHongbao(product));
	   							vo.setIsServenBack(product.getIsServenBack());
	   							vo.setIsFreePost(product.getIsFreePost());
	   							String totalHongbaoTemp = NumberStringHelper.mulString(temp[2], productSku.getProductHongbao());
	   							totalHongbao = NumberStringHelper.addString(totalHongbao, totalHongbaoTemp);
	   							
	   							String totalPriceTemp = NumberStringHelper.mulString(temp[2], productSku.getProductPrice());
	   							totalPrice = NumberStringHelper.addString(totalPrice, totalPriceTemp);
	   							
	   							String totalOriginPriceTemp = NumberStringHelper.mulString(temp[2], productSku.getProductOrginPrice());
	   							totalOriginPrice = NumberStringHelper.addString(totalOriginPrice, totalOriginPriceTemp);
	   							
	   							productsDetail.add(vo);
	   						}
		   					}
		   					
		   					Map<String, Object> dataMap = new HashMap<String,Object>(); 
			   			    dataMap.put("products", productsDetail);
			   			    dataMap.put("mainAddress", mainAddress);
			   			    dataMap.put("paramProducts", products);
			   			    dataMap.put("paramMarketUserProductId", marketUserProductId);
			   			    dataMap.put("isHaveAddress", isHaveAddress);
			   			    
			   			    dataMap.put("totalHongbao", totalHongbao);
			   			    dataMap.put("totalOriginPrice", totalOriginPrice);
			   			    dataMap.put("totalPostFee", totalPostFee);
			   			    dataMap.put("totalPrice", totalPrice);
			   			    resultMap.put(DATA, dataMap);
			   			    result = SUCCESS;
			   			    errorCode = SUCCESS_ERROR_CODE;
			   			 	errorMsg = SUCCESS_ERROR_MSG; 
		   				}else{
	   					result = FAIL;
	           			errorCode = PRODUCT_EMPTY_ERROR_CODE;
	                   	errorMsg = PRODUCT_EMPTY_ERROR_MSG; 
	   			    }
   				}
	   			    
	   			}else{
					result = LOGIN;
	    			errorCode = LOGIN_ERROR_CODE;
	            	errorMsg = LOGIN_ERROR_MSG;  	
				}
			}
	   		
		}catch(Exception e){
			errorCode = SERVER_ERROR_CODE;
	    	errorMsg = SERVER_ERROR_MSG;
	    	//log(e);
	    	logException("MallOrderInterface", "preCreate", e.toString(),"预产生订单",e);
   		
		}
		resultMap.put(RESULT, result);
		resultMap.put(ERROR_CODE, errorCode);
		resultMap.put(ERROR_MSG, errorMsg);
		isCrrossDomain(response);
		return resultMap;
   }
   
   /**
    * 添加商品款式到购物车
    * @return
    */
   @RequestMapping(value="/update")
   @ResponseBody
   public Map<String,Object> update(HttpServletRequest request,HttpServletResponse response,String token,String productSkuId,String number){
   	Map<String, Object> resultMap = new HashMap<String,Object>();
   	String result = FAIL;
   	String errorCode = PARAM_ERROR_CODE;
   	String errorMsg = PARAM_ERROR_MSG;
   	try{
   		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productSkuId) && StringHelper.isNotBlank(number) ){
   			MallUser user = getMallUserByToken(token);
   			if( null != user ){
   				
   				ProductSku productSku = productSkuService.get(productSkuId);
   				if( null != productSku ){
   				    //更新购物车商品信息
   	   				
   	   			    //更新购物车信息
   					result = updateProductSku(getUserCert(user.getId()),productSkuId,number);
   					//result=addOneProductSku(getUserCert(user.getId()),productSkuId);
   		   			errorCode = SUCCESS_ERROR_CODE;
   		   	       	errorMsg = SUCCESS_ERROR_MSG; 
   				}else{
   					errorCode = SKU_NULL_ERROR_CODE;
   	   		       	errorMsg = SKU_NULL_ERROR_MSG; 
   				}
	   	   		
   			}else{
   				result = LOGIN;
   				errorCode = LOGIN_ERROR_CODE;
   		       	errorMsg = LOGIN_ERROR_MSG;    
   			}
   		}
       	
   	}catch(Exception e){
   		errorCode = SERVER_ERROR_CODE;
       	errorMsg = SERVER_ERROR_MSG;
       	logException("MallCompanyInterface", "phone", e.toString(),"获取客服电话",e);
   	}
   	resultMap.put(RESULT, result);
   	resultMap.put(ERROR_CODE, errorCode);
   	resultMap.put(ERROR_MSG, errorMsg);
   	isCrrossDomain(response);
   	return resultMap;
   } 
   
   
   /**
    * 添加商品款式到购物车
    * @return
    */
   @RequestMapping(value="/add")
   @ResponseBody
   public Map<String,Object> add(HttpServletRequest request,HttpServletResponse response,String token,String productSkuId){
   	Map<String, Object> resultMap = new HashMap<String,Object>();
   	String result = FAIL;
   	String errorCode = PARAM_ERROR_CODE;
   	String errorMsg = PARAM_ERROR_MSG;
   	try{
   		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productSkuId) ){
   			MallUser user = getMallUserByToken(token);
   			if( null != user ){
   				
   				ProductSku productSku = productSkuService.get(productSkuId);
   				if( null != productSku ){
   					result=addOneProductSku(getUserCert(user.getId()),productSkuId);
   		   			errorCode = SUCCESS_ERROR_CODE;
   		   	       	errorMsg = SUCCESS_ERROR_MSG; 
   				}else{
   					errorCode = SKU_NULL_ERROR_CODE;
   	   		       	errorMsg = SKU_NULL_ERROR_MSG; 
   				}
	   	   		
   			}else{
   				result = LOGIN;
   				errorCode = LOGIN_ERROR_CODE;
   		       	errorMsg = LOGIN_ERROR_MSG;    
   			}
   		}
       	
   	}catch(Exception e){
   		errorCode = SERVER_ERROR_CODE;
       	errorMsg = SERVER_ERROR_MSG;
       	logException("MallCompanyInterface", "phone", e.toString(),"获取客服电话",e);
   	}
   	resultMap.put(RESULT, result);
   	resultMap.put(ERROR_CODE, errorCode);
   	resultMap.put(ERROR_MSG, errorMsg);
   	isCrrossDomain(response);
   	return resultMap;
   } 
   
   /**
    * 减少商品款式到购物车
    * @return
    */
   @RequestMapping(value="/sub")
   @ResponseBody
   public Map<String,Object> sub(HttpServletRequest request,HttpServletResponse response,String token,String productSkuId){
   	Map<String, Object> resultMap = new HashMap<String,Object>();
   	String result = FAIL;
   	String errorCode = PARAM_ERROR_CODE;
   	String errorMsg = PARAM_ERROR_MSG;
   	try{
   		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productSkuId) ){
   			MallUser user = getMallUserByToken(token);
   			if( null != user ){
   				
   				ProductSku productSku = productSkuService.get(productSkuId);
   				if( null != productSku ){
   					result=subOneProductSku(getUserCert(user.getId()),productSkuId);
   		   			errorCode = SUCCESS_ERROR_CODE;
   		   	       	errorMsg = SUCCESS_ERROR_MSG; 
   				}else{
   					errorCode = SKU_NULL_ERROR_CODE;
   	   		       	errorMsg = SKU_NULL_ERROR_MSG; 
   				}
	   	   		
   			}else{
   				result = LOGIN;
   				errorCode = LOGIN_ERROR_CODE;
   		       	errorMsg = LOGIN_ERROR_MSG;    
   			}
   		}
       	
   	}catch(Exception e){
   		errorCode = SERVER_ERROR_CODE;
       	errorMsg = SERVER_ERROR_MSG;
       	logException("MallCompanyInterface", "phone", e.toString(),"获取客服电话",e);
   	}
   	resultMap.put(RESULT, result);
   	resultMap.put(ERROR_CODE, errorCode);
   	resultMap.put(ERROR_MSG, errorMsg);
   	isCrrossDomain(response);
   	return resultMap;
   } 
   
   /**
    * 删除商品款式到购物车
    * @return
    */
   @RequestMapping(value="/del")
   @ResponseBody
   public Map<String,Object> del(HttpServletRequest request,HttpServletResponse response,String token,String productSkuIds){
	   	Map<String, Object> resultMap = new HashMap<String,Object>();
	   	String result = FAIL;
	   	String errorCode = PARAM_ERROR_CODE;
	   	String errorMsg = PARAM_ERROR_MSG;
	   	try{
	   		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(productSkuIds) ){
	   			
	   			MallUser user = getMallUserByToken(token);
	   			if( null != user ){
	   				String[] split = productSkuIds.split(",");
	   				List<String> skuIds = new ArrayList<String>();
	   				for( int i=0; i<split.length; i++ ){
	   					skuIds.add(split[i]);
	   				}
	   				
	   				CertProductExample example = new CertProductExample();
	   				Criteria criteria = example.createCriteria();
	   				criteria.andUserIdEqualTo(user.getId());
	   				criteria.andProductSkuIdIn(skuIds);
	   				
	   				List<CertProduct> list = certProductService.list(example);
	   				if( list.size() > 0 ){
	   					result = delProductSkus(getUserCert(user.getId()),list);
	   		   			errorCode = SUCCESS_ERROR_CODE;
	   		   	       	errorMsg = SUCCESS_ERROR_MSG; 
	   				}else{
	   					result = SUCCESS;
	   					errorCode = SUCCESS_ERROR_CODE;
	   		   	       	errorMsg = SUCCESS_ERROR_MSG; 
	   				}
	   			}else{
	   				result = LOGIN;
	   				errorCode = LOGIN_ERROR_CODE;
	   		       	errorMsg = LOGIN_ERROR_MSG;    
	   			}
	   		}
	       	
	   	}catch(Exception e){
	   		errorCode = SERVER_ERROR_CODE;
	       	errorMsg = SERVER_ERROR_MSG;
	       	logException("MallCompanyInterface", "phone", e.toString(),"获取客服电话",e);
	   	}
	   	resultMap.put(RESULT, result);
	   	resultMap.put(ERROR_CODE, errorCode);
	   	resultMap.put(ERROR_MSG, errorMsg);
	   	isCrrossDomain(response);
	   	return resultMap;
   } 
   
   //删除购物车信息
   public String delProductSkus( Cert cert , List<CertProduct> list ){
	   for( CertProduct tmp :list ){
		   certProductService.delete(tmp);
	   }
	   reSetCert(cert);
	   return SUCCESS;
   }
   
    
   /**
    * 获得用户购物车信息
    * @param userId
    * @return
    */
   public Cert getUserCert( String userId ){
      Cert cert = null;
      CertExample example = new CertExample();
      example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andUserIdEqualTo(userId);
      List<Cert> list = certService.list(example);
      if( list.size() > 0 ){
   	    cert = list.get(0);
      }else{
   	    cert = new Cert();
   	    cert.setUserId(userId);
   	    cert.setTotalCount("0");
   	    cert.setTotalMoney("0");
   	    certService.add(cert);
      }
      return cert;
   }
   
   /**
    * 获取用户购物车商品信息
    * @param cert
    * @return
    */
   public List<CertProduct> getUserCertProduct( Cert cert ){
	   CertProductExample example = new CertProductExample();
	   example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andCertIdEqualTo(cert.getId());
	   return certProductService.list(example);
   }
   
   /**
    * 添加商品
    * @param cert
    * @param productSkuId
    * @return
    */
   public String updateProductSku( Cert cert, String productSkuId, String number ){
	    String result = SUCCESS;
	   	CertProductExample example = new CertProductExample();
	   	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductSkuIdEqualTo(productSkuId).andCertIdEqualTo(cert.getId());
	   	List<CertProduct> list = certProductService.list(example);
	   	if( list.size() > 0 ){
	   		//判断有没有这个商品,有没有领取红包,
	   		//数量加1
	   		CertProduct certProduct = list.get(0);
	   		certProduct.setProductCount(number);
	   		certProductService.edit(certProduct);
	   		reSetCert(cert);
	   	}
	    return result;
   }

   /**
    * 添加商品
    * @param cert
    * @param productSkuId
    * @return
    */
   public String addOneProductSku( Cert cert, String productSkuId ){
	    String result = "fail";
	   	CertProductExample example = new CertProductExample();
	   	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductSkuIdEqualTo(productSkuId).andCertIdEqualTo(cert.getId());
	   	List<CertProduct> list = certProductService.list(example);
	   	if( list.size() > 0 ){
	   		//判断有没有这个商品,有没有领取红包,
	   		
	   		//数量加1
	   		CertProduct certProduct = list.get(0);
	   		String productCount = NumberStringHelper.addString(certProduct.getProductCount(), "1");
	   		certProduct.setProductCount(productCount);
	   		certProductService.edit(certProduct);
	   		
	   		
	   		cert.setTotalCount(cert.getTotalCount()+1);
	   		//价格得重新算下
	   		cert.setTotalMoney(getCertTotalMoney(cert));
	   		result =certService.edit(cert);
	   	}else{
	   		ProductSku sku = productSkuService.get(productSkuId);
	   		//sku = filterSeckillPrice(sku);
	   		Product product = productService.get(sku.getProductId());
	   		//添加购物车数量 ，总额，判断有没有红包
	   		CertProduct certProduct = new CertProduct();
	   		certProduct.setCertId(cert.getId());
	   		certProduct.setUserId(cert.getUserId());
	   		
	   		certProduct.setProductCount("1");
	   		certProduct.setProductSkuId(productSkuId);
	   		certProduct.setProductId(sku.getProductId());
	   		certProduct.setProductMainImageUrl(product.getMainImageUrl());
	   		certProduct.setProductHongbao(getHongbao(product));
	   		certProduct.setProductName(product.getName());
	   		certProduct.setProductOrginPrice(sku.getProductOrginPrice());
	   		
	   		certProduct.setProductPrice( getProdcutSkuPrice(sku) );
	   		
	   		cert.setTotalCount(cert.getTotalCount()+1);
	   		//价格得重新算下
	   		cert.setTotalMoney(getCertTotalMoney(cert));
	   		certService.edit(cert);
	   		
	   		result = certProductService.add(certProduct);
	   	}
	    return result;
   }
   
   /**
    * 减少商品
    * @param cert
    * @param productSkuId
    * @return
    */
   public String subOneProductSku( Cert cert, String productSkuId ){
	    String result = SUCCESS;
	   	CertProductExample example = new CertProductExample();
	   	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductSkuIdEqualTo(productSkuId).andCertIdEqualTo(cert.getId());
	   	List<CertProduct> list = certProductService.list(example);
	   	if( list.size() > 0 ){
	   		//判断有没有这个商品,有没有领取红包,
	   		//数量加1
	   		CertProduct certProduct = list.get(0);
	   		
	   		String count = NumberStringHelper.subtractString(certProduct.getProductCount(),"1");
	   		if( NumberStringHelper.isGreaterNoEqual(count, "0") ){
	   			certProduct.setProductCount(count);
		   		certProductService.edit(certProduct);
	   		}else{
	   			certProductService.delete(certProduct);
	   		}
	   		cert.setTotalCount(NumberStringHelper.subtractString(cert.getTotalCount(), "1"));
	   		//价格得重新算下
	   		cert.setTotalMoney(getCertTotalMoney(cert));
	   		certService.edit(cert);
	   	}
	    return result;
   }
   
   
   //重新算一下购物的总价格
   public String getCertTotalMoney( Cert cert ){
	   String result="0";
	   CertProductExample example = new CertProductExample();
	   Criteria criteria = example.createCriteria();
	   criteria.andIsEffectiveEqualTo(DBConst.TRUE).andCertIdEqualTo(cert.getId());
	   List<CertProduct> list = certProductService.list(example);
	   if( list.size() > 0 ){
		   for( CertProduct tmp : list ){
			   String productSkuId = tmp.getProductSkuId();
			   ProductSku sku = productSkuService.get(productSkuId);
			   Product product = productService.get(sku.getProductId());
			   String productCount = tmp.getProductCount();
			   String price = sku.getProductOrginPrice();
			   String haveHongbao = isHaveHongbao(cert.getUserId(),sku.getProductId());
			   if( "1".equals(haveHongbao) ){
				   price= NumberStringHelper.subtractString(sku.getProductOrginPrice(), product.getProductHongbao());
			   }
			   result=NumberStringHelper.mulString(price, productCount);
		   }
	   }
	   return result;
   }
   
   //重新算一下购物的总价格
   public Cert reSetCert( Cert cert ){
	   String totalCount="0";
	   String totalMoney="0";
	   CertProductExample example = new CertProductExample();
	   Criteria criteria = example.createCriteria();
	   criteria.andIsEffectiveEqualTo(DBConst.TRUE).andCertIdEqualTo(cert.getId());
	   List<CertProduct> list = certProductService.list(example);
	   if( list.size() > 0 ){
		   for( CertProduct tmp : list ){
			   String productSkuId = tmp.getProductSkuId();
			   ProductSku sku = productSkuService.get(productSkuId);
			   Product product = productService.get(sku.getProductId());
			   String productCount = tmp.getProductCount();
			   String price = sku.getProductOrginPrice();
			   String haveHongbao = isHaveHongbao(cert.getUserId(),sku.getProductId());
			   if( "1".equals(haveHongbao) ){
				   price= NumberStringHelper.subtractString(sku.getProductOrginPrice(), product.getProductHongbao());
			   }
			   
			   String tempTotalMoney = NumberStringHelper.mulString(price, productCount);
			   totalMoney = NumberStringHelper.addString(totalMoney, tempTotalMoney);
			   
			   totalCount = NumberStringHelper.addString(totalCount, productCount);
			   
		   }
	   }
	   
	   cert.setTotalMoney(totalMoney);
	   cert.setTotalCount(totalCount);
	   certService.edit(cert);
	   return cert;
   }
   
}